package com.formina.stars;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import com.formina.engine.Engine;
import com.formina.engine.Model;

public class CometStreak extends Model {
	int rotationAxis;
	FloatBuffer fb;
	public CometStreak(float[] location, float[] scale){
 	   super(location, scale,Engine.StarParticle);
 	  ByteBuffer byteBuf = ByteBuffer.allocateDirect(3 * 4);
      byteBuf.order(ByteOrder.nativeOrder());
      fb = byteBuf.asFloatBuffer();
      fb.put(location);
      fb.position(0);
    }
	   
	   public CometStreak() {
		  this(new float[]{0,0,0}, new float[]{1f,1f,1f});
	   }
	   
	   @Override
	   public void update(long time){
		   setAlpha(Color()[3]-.02f);;
		   Rotation()[rotationAxis] += 4f;
		   if (Rotation()[rotationAxis]>359){
			   Rotation()[rotationAxis]=0;
		   }
		   if(this.Color()[3] <=0){
			   this.isDead = true;
		   }
	   }
	   
	   @Override
	    public void draw(GL10 gl) {
	    	gl.glDisable(GL10.GL_LIGHTING);
	    	gl.glPushMatrix();
	        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

	    	// Counter-clockwise winding.
			gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
			// Enable face culling.
			gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs
			// What faces to remove with the face culling.
			gl.glCullFace(GL10.GL_BACK); // OpenGL docs
			gl.glEnable(GL10.GL_ALPHA);
			gl.glEnable(GL10.GL_ALPHA_TEST);
			
			gl.glTranslatef(getX(), getY(),getZ());
			gl.glRotatef(Rotation()[0],1f,0,0);
			gl.glRotatef(Rotation()[1],0,1f,0);
			gl.glRotatef(Rotation()[2],0,0,1f);
			gl.glScalef(Scale()[0], Scale()[1], Scale()[2]);
			gl.glColor4f(this.Color()[0],this.Color()[1],this.Color()[2],this.Color()[3]);

			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

			gl.glDrawElements(GL10.GL_POINTS,3,// OpenGL docs
	    		  GL10.GL_UNSIGNED_SHORT,fb);
			
	      		      
	      
			gl.glDisable(GL10.GL_CULL_FACE);
			gl.glDisable(GL10.GL_ALPHA);

			if(modelInfo.Normals.length>0){
				gl.glDisable(GL10.GL_NORMALIZE);
				gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
			}
	    	gl.glPopMatrix();
	    	gl.glEnable(GL10.GL_LIGHTING);


	   }
}
